package cc.rengu.igas.smps.biz.service.impl;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.OcrRecogService;
import cc.rengu.igas.smps.facade.request.BankCardRecogRequest;
import cc.rengu.igas.smps.facade.request.BusinessLicenseRecogRequest;
import cc.rengu.igas.smps.facade.request.IdentityCertRecogRequest;
import cc.rengu.igas.smps.facade.response.BankCardRecogResponse;
import cc.rengu.igas.smps.facade.response.BusinessLicenseRecogResponse;
import cc.rengu.igas.smps.facade.response.IdentityCertRecogResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by WKF on 2020-04-10
 *
 * @author WKF
 */
public class OcrRecogServiceImpl implements OcrRecogService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 营业执照OCR文字识别
     *
     * @param businessLicenseRecogRequest 营业执照信息
     * @return Result<BusinessLicenseRecogResponse> 营业执照信息识别结果
     */
    @Override
    public Result<BusinessLicenseRecogResponse> businessLicenseRecog(BusinessLicenseRecogRequest businessLicenseRecogRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BusinessLicenseRecogResponse> result = new Result<>();
        BusinessLicenseRecogResponse businessLicenseRecogResponse = new BusinessLicenseRecogResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(businessLicenseRecogRequest, SmpsServiceConstant.OCR_BUSINESS_LICENSE_RECOG_SERVICE, BusinessLicenseRecogResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BusinessLicenseRecogResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("营业护照OCR识别调用失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                businessLicenseRecogResponse.setRespCode(((BizException) e).getCode());
                businessLicenseRecogResponse.setRespDesc(e.getMessage());
            } else {
                businessLicenseRecogResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                businessLicenseRecogResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 身份证OCR文字识别
     *
     * @param identityCertRecogRequest 身份证件信息
     * @return Result<IdentityCertRecogResponse> 身份证件信息识别结果
     */
    @Override
    public Result<IdentityCertRecogResponse> identityCertRecog(IdentityCertRecogRequest identityCertRecogRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<IdentityCertRecogResponse> result = new Result<>();
        IdentityCertRecogResponse identityCertRecogResponse = new IdentityCertRecogResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(identityCertRecogRequest, SmpsServiceConstant.OCR_IDENTITY_CERT_RECOG_SERVICE, IdentityCertRecogResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((IdentityCertRecogResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("身份证件OCR识别调用失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                identityCertRecogResponse.setRespCode(((BizException) e).getCode());
                identityCertRecogResponse.setRespDesc(e.getMessage());
            } else {
                identityCertRecogResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                identityCertRecogResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 银行卡OCR文字识别
     *
     * @param bankCardRecogRequest 银行卡信息
     * @return Result<BankCardRecogResponse> 银行卡信息识别结果
     */
    @Override
    public Result<BankCardRecogResponse> bankCardRecog(BankCardRecogRequest bankCardRecogRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardRecogResponse> result = new Result<>();
        BankCardRecogResponse bankCardRecogResponse = new BankCardRecogResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardRecogRequest, SmpsServiceConstant.OCR_BANK_CARD_RECOG_SERVICE, BankCardRecogResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardRecogResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡OCR识别调用失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardRecogResponse.setRespCode(((BizException) e).getCode());
                bankCardRecogResponse.setRespDesc(e.getMessage());
            } else {
                bankCardRecogResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardRecogResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
